<template>
    <div>
        <global-tables
            :total="total"
            :list="list"
            :loading="loading"
            :columns="columns"
            :current="search.Page"
            @changePage="handleChangePage"
            @handleAllSelect="handleAllSelect"
        >
            <Form :model="search" :label-width="80">
                <div class="table-top-wrap table-top-margin"></div>
            </Form>
        </global-tables>
        <view-approval v-if="isViewApproval" :id="itemId" :isApproval="isViewApproval" @handleCloseViewApproval="handleCloseViewApproval"></view-approval>
    </div>
</template>

<script>
/**
 * 审批列表
 */
import { cyclicRouting } from '@/util';
import { getModulePermission } from '@/api/user';
import { getTablesList } from './service';
import GlobalTables from '@/components/GlobalTables';
import ViewApproval from '@/components/ViewApproval';

export default {
    name: 'FlowHistory',
    components: {
        GlobalTables,
        ViewApproval
    },
    data() {
        return {
            list: [],
            itemId: 0,
            allSelectArr: null,
            loading: true,
            isViewApproval: false,
            isView: false,
            total: 0,
            isModal: false,
            search: {
                keyword: '',
                pageindex: 1,
                pagesize: 10
            },
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '标题',
                    key: 'Title'
                },
                {
                    title: '创建人',
                    key: 'ProPoserName'
                },
                {
                    title: '原因',
                    key: 'Reason'
                },
                {
                    title: '图片',
                    width: '100px',
                    render: (h, params) => {
                        const { UploadPic } = params.row;
                        if (UploadPic) {
                            return h('div', [
                                h('Icon', {
                                    props: {
                                        type: 'ios-checkmark-circle-outline',
                                        color: '#1abc9c',
                                        size: '18'
                                    }
                                })
                            ]);
                        } else {
                            return h('div', [
                                h('Icon', {
                                    props: {
                                        type: 'ios-close-circle-outline',
                                        color: 'red',
                                        size: '18'
                                    }
                                })
                            ]);
                        }
                    }
                },
                {
                    title: '文件',
                    width: '100px',
                    render: (h, params) => {
                        const { UploadFile } = params.row;
                        if (UploadFile) {
                            return h('div', [
                                h('Icon', {
                                    props: {
                                        type: 'ios-checkmark-circle-outline',
                                        color: '#1abc9c',
                                        size: '18'
                                    }
                                })
                            ]);
                        } else {
                            return h('div', [
                                h('Icon', {
                                    props: {
                                        type: 'ios-close-circle-outline',
                                        color: 'red',
                                        size: '18'
                                    }
                                })
                            ]);
                        }
                    }
                },
                {
                    title: '操作',
                    width: '100px',
                    align: 'center',
                    render: (h, params) => {
                        const { isView } = this;
                        return h('div', [
                            isView &&
                                h(
                                    'Button',
                                    {
                                        props: {
                                            type: 'info',
                                            size: 'small'
                                        },
                                        style: {
                                            background: '#5ab1ef',
                                            border: 'none',
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.handleBtnClick(params.row, 'view');
                                            }
                                        }
                                    },
                                    '查看'
                                )
                        ]);
                    }
                }
            ]
        };
    },
    methods: {
        /* 表格数据请求 */
        getTablesListData() {
            const data = this.search;
            getTablesList(data).then(res => {
                this.list = res.rows;
                this.loading = false;
                this.total = res.records;
            });
        },
        /* 表格查询 */
        handleSearch() {
            this.loading = true;
            this.getTablesListData();
        },
        /* 表格分页方法 */
        handleChangePage(type, page) {
            this.loading = true;
            if (type == 'page') {
                this.search.pageindex = page;
                this.getTablesListData();
            } else {
                this.search.pagesize = page;
                this.getTablesListData();
            }
        },
        /* 关闭查看 */
        handleCloseViewApproval() {
            this.isViewApproval = false;
        },
        /* 表单数据 */
        initFormItem() {
            return {};
        },
        /* 复选框选中 */
        handleAllSelect(list) {
            let arr = [];
            for (let item in list) {
                arr.push(list[item].ID);
                arr.join(',');
            }
            arr = arr.join(',');
            this.allSelectArr = arr;
        },
        /* 对话框 */
        handleBtnClick(item, type) {
            this.itemId = item.ID;
            if (type == 'view') {
                this.isViewApproval = true;
            }
        }
    },
    mounted() {
        /* 获取菜单的id */
        cyclicRouting(JSON.parse(sessionStorage.getItem('user')).menuList, this.$route, id => {
            /* 按钮权限请求 */
            getModulePermission({
                roleid: JSON.parse(sessionStorage.getItem('user')).roleid,
                moduleid: id
            }).then(res => {
                /* 按钮权限赋值 */
                for (let item in res.data) {
                    if (res.data[item].name == 'view') {
                        this.isView = true;
                    }
                }
                /* 列表请求 */
                this.getTablesListData();
            });
        });
    }
};
</script>

<style>
</style>
